package com.gz.mylife.app;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;

import com.gz.mylife.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.zl.smartmall.library.util.CrashHandler;

public class HDPictureBrowse extends BaseActivity implements OnLongClickListener {
	
	public static final String TAG = HDPictureBrowse.class.getSimpleName();
	
	private DisplayImageOptions mOptions;
	private FragmentAdapter mFragmentAdapter;
	private List<Fragment> mFragments;
	private List<ImageView> mImgView;
	private List<String> mPictures;
	private ViewPager mViewPager;
	private TextView tv_close;//关闭
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_hdpicture_browse);
		
		mFragmentAdapter = new FragmentAdapter(getSupportFragmentManager());
		mFragments = new ArrayList<Fragment>();
		mImgView = new ArrayList<ImageView>();
		mOptions = buildImageOptions();
		mPictures = getIntent().getStringArrayListExtra("picture");
		init();
		mViewPager = (ViewPager) findViewById(R.id.viewpager);
		mViewPager.setAdapter(mFragmentAdapter);
		mViewPager.setCurrentItem(getIntent().getIntExtra("position", 0), true);
		mActionBar.hide();
		tv_close = (TextView) findViewById(R.id.tv_close);
		tv_close.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}

	@Override
	public boolean onLongClick(View v) {
		// TODO Auto-generated method stub
		return false;
	}
	
	private DisplayImageOptions buildImageOptions() {
		return new DisplayImageOptions.Builder()
			.showImageOnLoading(R.color.transparent)
			.showImageOnFail(R.color.transparent)
			.showImageForEmptyUri(R.color.transparent)
			.resetViewBeforeLoading(true)
			.bitmapConfig(Bitmap.Config.ARGB_8888)
			.cacheInMemory(true)
			.cacheOnDisk(true)
			.imageScaleType(ImageScaleType.IN_SAMPLE_INT) // default
//			.displayer(new CircularBitmapDisplayer())
			.build();
	}
	
	private void init() {
		for(int i = 0; i < mPictures.size(); i++) {
			ImageView view = new ImageView(this);
			view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
//			view.setScaleType(ScaleType.FIT_XY);
//			view.setId(R.id.hd_picture_view);
			view.setOnLongClickListener(this);
			mImgView.add(view);
			mFragments.add(PictureFragment.newInstance(i));
		}
	}

	private class FragmentAdapter extends FragmentStatePagerAdapter {
		
		public FragmentAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			return mFragments.get(position);
		}

		@Override
		public int getCount() {
			return mFragments.size();
		}
		
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			super.destroyItem(container, position, object);
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {

			Object obj = super.instantiateItem(container, position);
			CrashHandler.d(TAG, "FragmentAdapter's instantiateItem");
			return obj;
		}
	}
	
	public static class PictureFragment extends Fragment { 
		private int mPosition;
		
		static PictureFragment newInstance(int position){
			PictureFragment fragment = new PictureFragment();
			Bundle bundle = new Bundle();
			bundle.putInt("position", position);
			fragment.setArguments(bundle);
			
			return fragment;
		}
		
		public void setPosition(int position){
			mPosition = position;
		}
		
		@Override
		public void onCreate(Bundle savedInstanceState) {
			// TODO Auto-generated method stub
			super.onCreate(savedInstanceState);
			CrashHandler.d(TAG, "AdvertFragment onCreate");
			mPosition = getArguments().getInt("position");
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			
			ImageLoader.getInstance().displayImage(
					((HDPictureBrowse) getActivity()).mPictures.get(mPosition),
					((HDPictureBrowse) getActivity()).mImgView.get(mPosition), 
					((HDPictureBrowse) getActivity()).mOptions);
			
		
			return ((HDPictureBrowse) getActivity()).mImgView.get(mPosition);
		}

		@Override
		public void onActivityCreated(Bundle savedInstanceState) {
			// TODO Auto-generated method stub
			super.onActivityCreated(savedInstanceState);
			CrashHandler.d(TAG, "AdvertFragment onActivityCreated");
		}

		@Override
		public void onDestroyView() {
			super.onDestroyView();
			
			CrashHandler.d(TAG, "Fragment's onDestroyView");
			
//			if(mPosition >= ((Community)getActivity()).mAdvertViews.size()){
//				return;
//			}
			HDPictureBrowse mFrg = (HDPictureBrowse) getActivity();
			if(mFrg != null && mFrg.mImgView.get(mPosition) != null
					&& mFrg.mImgView.get(mPosition).getParent() != null){
				((ViewGroup)mFrg.mImgView.get(mPosition).getParent())
					.removeView(mFrg.mImgView.get(mPosition));
			
			}	
		}
		
		@Override
		public void onSaveInstanceState(Bundle outState) {
			// TODO Auto-generated method stub
			super.onSaveInstanceState(outState);
			outState.putInt("position", mPosition);
		}
	}
	
}
